<?php
	require_once 'UserService.class.php';
	
	//接收用户名和密码
	$loginUser = $_POST['username'];
	$passwd = $_POST['password'];
	$captcha = $_POST['captcha'];

	//准备工作
	session_start();
	$userServ = new UserService();
	
	if(!empty($_GET['type']) && $_GET['type'] == "logout"){
		$userServ->userLogout($_SESSION['loginuser']);
		//销毁session
		session_destroy();
		header("Location: login.php");
		exit();
	}
	
	
	//获取对应用户名密码
	$arr = $userServ->getUserByName($loginUser);
	if(!empty($arr)){
		if(md5($captcha) != $_SESSION['captcha'])
		{
			$_SESSION['login_err'] = "验证码错误！";
			header("Location: login.php");
			exit();
		}
		if(md5($passwd) == $arr['passwd']){
			$userServ->userLogin($loginUser);
			$_SESSION['loginuser'] = $loginUser;
			header("Location: friendList.php");
			exit();	
		}
	}
	header("Location: login.php");
	exit();

?>
